<?php


namespace app\project\model;


use app\user\model\User;
use think\Model;
use think\model\concern\SoftDelete;
use util\Tree;

class Category extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $defaultSoftDelete = 0;

    protected $name = 'project_category';
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    // 设置json类型字段
    protected $json = ['setting'];

    public function adminUser()
    {
        return $this->hasOne(User::class,'id','principal')->bind(['principal_name'=>'username']);
    }

    public function getTreeList($id = 0, $default = ''){
        $result[0] = '顶级栏目';

        $where = [
            'pid'=>0
        ];

        $data_list = Tree::toList(self::where($where)->order('pid,id')->column('id,pid,title'));
        foreach ($data_list as $item) {
            $result[$item['id']] = $item['title_display'];
        }

        return $result;
    }
}